package com.say.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.say.bean.common.Response;
import com.say.bean.domain.Location;
import com.say.bean.domain.Message;
import com.say.bean.domain.Trace;
import com.say.bean.domain.User;
import com.say.service.ChatService;
import com.say.service.LocationService;
import com.say.service.MessageService;
import com.say.service.ShareService;
import com.say.service.TraceService;
import com.say.service.UserService;
import com.say.utils.UserUtil;

@Controller
@RequestMapping("/wensay/home")
public class ApiHomeController extends BaseController {
	
	@Autowired
	protected UserService userService;
	
	@Autowired
	protected ChatService chatService;
	
	@Autowired
	protected MessageService messageService;
	
	 @Autowired
	 public ShareService shareService;
	 
	 @Autowired
	 public LocationService locationService;
	 
	 @Autowired
	 public TraceService traceService;
	
	
	//我的页面
	@RequestMapping()
	public String home(Model model){
		User user = UserUtil.getUser(request);
		model.addAttribute("user", user);
		model.addAttribute("distance", locationService.getDistanceByUserId("100", "101"));
		traceService.save(new Trace(user.getId(),"0.0.0.0","访问我的页面"));
		return "wensay/home";
	}
	
	
	//我的页面
	@RequestMapping("/my")
	public String my(Model model){
		User user = UserUtil.getUser(request);
		model.addAttribute("user", user);
		return "wensay/my";
	}
	
	@RequestMapping(value="/mood/save")
	@ResponseBody
	public Response moodSave(Model model,String mood) { 
		Response response = new  Response();
		User user = UserUtil.getUser(request);
		String userId=user.getId();
		userService.updateMoodById(userId, mood);
		HttpSession session = request.getSession();
		user=userService.getById(userId);
        session.setAttribute("user",user);
		response.success("操作成功");
		return response;
	}
	
	
	
	@RequestMapping(value="/seal/save")
	@ResponseBody
	public Response sealSave(Model model,String mood) { 
		Response response = new  Response();
		User user = UserUtil.getUser(request);
		//String userId=user.getId();
		
		shareService.updateShareStautsByUser("100");
		chatService.updateChatStautsByUser("100");
		
		shareService.updateShareStautsByUser("101");
		chatService.updateChatStautsByUser("101");
		
		Message message=messageService.getMessageByUserIdAndToUserId("100", "101");
		if(message!=null) {
			messageService.saveUnReadMessageById(message.getId(), 0, "");
		}
		message=messageService.getMessageByUserIdAndToUserId("101", "100");
		if(message!=null) {
			messageService.saveUnReadMessageById(message.getId(), 0, "");
		}
		
		response.success("操作成功");
		traceService.save(new Trace(user.getId(),"0.0.0.0","请求一键封存"));
		return response;
	}
	
	
	@RequestMapping(value="/location/save")
	@ResponseBody
	public Response locationSave(Model model,String latitude,String longitude,String speed,String accuracy) { 
		Response response = new  Response();
		User user = UserUtil.getUser(request);
		String userId=user.getId();
		user=userService.getById(userId);
		user.setLatitude(latitude);		
		user.setLongitude(longitude);
		userService.save(user);
		
		Location location=new Location();
		location.setUserId(userId);
		location.setLatitude(latitude);
		location.setLongitude(longitude);
		location.setSpeed(speed);
		location.setAccuracy(accuracy);
		location.setCreateTime(new Date());
		locationService.save(location);
		Map<String,Object> data=new HashMap<String, Object>();
		data.put("distance", locationService.getDistanceByUserId("100", "101"));
		response.success(data,"操作成功");
		return response;
	}
	
	@RequestMapping(value="/location/notsave")
	@ResponseBody
	public Response locationNotSave(Model model,String latitude,String longitude,String speed,String accuracy) { 
		Response response = new  Response();
		User user = UserUtil.getUser(request);
		String userId=user.getId();
		Location location=new Location();
		location.setUserId(userId);
		location.setLatitude(latitude);
		location.setLongitude(longitude);
		location.setSpeed(speed);
		location.setAccuracy(accuracy);
		location.setCreateTime(new Date());
		locationService.save(location);
		response.success("操作成功");
		return response;
	}
	
	
	


}
